package hibernate.method;

import hibernate.method.interfaces.CategoryMethodsInterface;
import hibernate.model.Category;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class CategoryMethods implements CategoryMethodsInterface{

	@Override
	public List<Category> listAllCategories(SessionFactory sessionFactory,
			Session session) {
		Criteria cr = session.createCriteria(Category.class);
		cr.addOrder(Order.asc("category"));
		@SuppressWarnings("unchecked")
		List<Category> clist = cr.list();
		return clist;
	}

	@Override
	public void addCategory(SessionFactory sessionFactory, Session session,
			String categoryName) {
		Category cat = new Category();
		cat.setCategory(categoryName);
		session.save(cat);
		
	}

	@Override
	public void deleteCategory(SessionFactory sessionFactory, Session session,
			Category cat) {
		session.delete(cat);
	}

	@Override
	public Category idToCategory(SessionFactory sessionFactory,
			Session session, int id) {
		Category cat = (Category) session.get(Category.class, new Integer(id));
		return cat;
	}

	@Override
	public Category findCategoryByName(SessionFactory sessionFactory,
			Session session, String categoryName) {
		Criteria cr = session.createCriteria(Category.class);
		
		Criterion cat = Restrictions.eq("category", categoryName);
		cr.add(cat);
		return (Category) cr.list().get(0);
	}

	
	
	
	
	
}
